#include "StdAfx.h"
#include "ProcessCommDLLMag.h"
#include "LogWrapper.h"

CProcessCommDLLMag::CProcessCommDLLMag(void)
	: m_hModule(NULL)
	, m_pfnCreateClient(NULL)
	, m_pfnCreateComm(NULL)
	, m_pfnCreateServer(NULL)
	, m_pfnDestroyClient(NULL)
	, m_pfnDestroyComm(NULL)
	, m_pfnDestroyServer(NULL)
{
}


CProcessCommDLLMag::~CProcessCommDLLMag(void)
{
	UninitializeIPC();
}

bool CProcessCommDLLMag::InitializeIPC( unsigned nVersion )
{
	bool bResult = false;
	switch(nVersion)
	{
	case IPC_VERSION_1:
		bResult = InitFormerVersion();
		break;
	case IPC_VERSION_2:
		bResult = InitNewVersion();
		break;
	default:
		break;
	}
	return bResult;
}

bool CProcessCommDLLMag::UninitializeIPC()
{
	bool bResult = false;
	if (m_hModule)
	{
		bResult = FreeLibrary(m_hModule)? true:false;
		m_hModule = NULL;
	}
	return bResult;
}

IProcessComm* CProcessCommDLLMag::CreateProcessComm()
{
	IProcessComm* pResult = NULL;
	if (m_pfnCreateComm)
	{
		pResult = (*m_pfnCreateComm)();
	}
	return pResult;
}

void CProcessCommDLLMag::DestroyProcessComm( IProcessComm* pComm)
{
	if (m_pfnDestroyComm)
	{
		m_pfnDestroyComm(pComm);
	}
}

IProcessServer* CProcessCommDLLMag::CreateProcessServer( const ServerInitParam& initParam )
{
	IProcessServer* pServer = NULL;
	if (m_pfnCreateServer)
	{
		pServer = (*m_pfnCreateServer)(initParam);
	}
	return pServer;
}

bool CProcessCommDLLMag::DestroyProcessServer( IProcessServer* pServer )
{
	bool bResult = false;
	if (m_pfnDestroyServer)
	{
		bResult = (*m_pfnDestroyServer)(pServer);
	}
	return bResult;
}

IProcessClient* CProcessCommDLLMag::CreateProcessClient( const ClientInitParam& initParam )
{
	IProcessClient* pClient = NULL;
	if (m_pfnCreateClient)
	{
		pClient = (*m_pfnCreateClient)(initParam);
	}
	return pClient;
}

bool CProcessCommDLLMag::DestroyProcessClient( IProcessClient* pClient )
{
	bool bResult = false;
	if (m_pfnDestroyClient)
	{
		bResult = (*m_pfnDestroyClient)(pClient);
	}
	return bResult;
}

bool CProcessCommDLLMag::InitFormerVersion()
{
	bool bResult = false;
#ifdef _WIN64
#ifdef _DEBUG
	m_hModule = LoadLibrary(L"ProcessComm64.dll");
#else
	m_hModule = LoadLibrary(L"ProcessComm64.dll");
#endif

#else
#ifdef _DEBUG
	m_hModule = LoadLibrary(L"ProcessCommD.dll");
#else
	m_hModule = LoadLibrary(L"ProcessComm.dll");
#endif
#endif	

	DWORD dwError = GetLastError();
	if (dwError != 0 && m_hModule == NULL)
	{
		CSingleton<CLogWrapper>::Instance()->Log(ERROR_LOG, L"Load ProcessComm module failed, error is %d.\n", dwError);
	}
	else
	{
		m_pfnCreateComm = (LPCreateProcessComm)GetProcAddress(m_hModule, "CreateProcessComm");
		m_pfnDestroyComm = (LPDestroyProcessComm)GetProcAddress(m_hModule, "DestroyProcessComm");
		if (m_pfnCreateComm && m_pfnDestroyComm)
		{
			bResult = true;
		}
		else
		{
			CSingleton<CLogWrapper>::Instance()->Log(ERROR_LOG, 
				L"Load ProcessComm functions failed.\n");
		}

	}
	return bResult;
}

bool CProcessCommDLLMag::InitNewVersion()
{
	bool bResult = false;
#ifdef _DEBUG
	m_hModule = LoadLibrary(L"ProcessCommV264.dll");
#else
	m_hModule = LoadLibrary(L"ProcessCommV264.dll");
#endif

	DWORD dwError = GetLastError();
	if (dwError != 0 && m_hModule == NULL)
	{
		CSingleton<CLogWrapper>::Instance()->Log(ERROR_LOG, L"Load ProcessComm2 module failed, error is %d.\n", dwError);
	}
	else
	{
		m_pfnCreateServer = (LPCreateProcessServer)GetProcAddress(m_hModule, "CreateProcessServer");
		m_pfnDestroyServer = (LPDestroyProcessServer)GetProcAddress(m_hModule, "DestroyProcessServer");

		m_pfnCreateClient = (LPCreateProcessClient)GetProcAddress(m_hModule, "CreateProcessClient");
		m_pfnDestroyClient = (LPDestroyProcessClient)GetProcAddress(m_hModule, "DestroyProcessClient");
		if (m_pfnCreateServer && m_pfnDestroyServer&& m_pfnCreateClient && m_pfnDestroyClient)
		{
			bResult = true;
		}
		else
		{
			CSingleton<CLogWrapper>::Instance()->Log(ERROR_LOG, 
				L"Load ProcessComm2 functions failed.\n");
		}
	}
	return bResult;
}